package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.CategoryServce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired(required = false)
    private CategoryServce categoryServce;
    @GetMapping("/page")
    public R<Page<Category>> findPage(Integer page, Integer pageSize){
        Page<Category> categoryList=categoryServce.findPage(page,pageSize);
        return R.success(categoryList);
    }
    //新增菜品分类
    /*
    * 添加分类
    * */
    @PostMapping
    public R<String> save(@RequestBody Category category, HttpSession session){
        //获取当前的登入者
        Long empId = (Long) session.getAttribute("employee");
        //补全category的信息
        category.setUpdateUser(empId);
        category.setCreateUser(empId);
        categoryServce.save(category);
        return R.success("保存菜品成功！");
    }
    @DeleteMapping
    public R<String> delete(Long id){
        categoryServce.delete(id);
        return R.success("删除菜品成功！");
    }
    @PutMapping
    public R<String> update(@RequestBody Category category, HttpServletRequest request){
        Long empId = (Long) request.getSession().getAttribute("employee");
        category.setUpdateUser(empId);
        categoryServce.update(category);
        return R.success("修改菜品成功！");
    }
    @GetMapping("/list")
    public R<List<Category>> list(Integer type){
        List<Category> listCategory =categoryServce.findByType(type);
        return R.success(listCategory);
    }


}
